<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :rules="rules" size="small" :inline="true">
      <el-form-item label="房间等级" prop="roomtypeId">
        <el-select v-model="queryParams.roomTypeId" placeholder="房间等级" clearable>
          <el-option
            v-for="item in typeOptions"
            :key="item.roomTypeId"
            :label="item.roomGrade"
            :value="item.roomTypeId"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:role:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="房间类型id" align="center" prop="roomTypeId" /> -->
      <el-table-column label="房间等级" align="center" prop="roomGrade" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="床位数" align="center" prop="bedQuantity" />
      <el-table-column label="总数量" align="center" prop="amount" />
      <el-table-column label="剩余数量" align="center" prop="remainingQuantity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:post:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['system:post:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      align="center"
      :current-page="pageNum"
      :page-sizes="[5, 10, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 添加房间类型配置对话框 -->
    <el-dialog :title="add" :visible.sync="openadd" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="床位数" prop="bedQuantity">
          <el-input v-model="form.bedQuantity" placeholder="请输入床位数" />
        </el-form-item>
        <!-- <el-form-item label="房间等级" prop="roomGrade">
          <el-select v-model="form.roomTypeId" placeholder="房间等级" clearable>
            <el-option
              v-for="item in typeOptions"
              :key="item.roomTypeId"
              :label="item.roomGrade"
              :value="item.roomTypeId"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="房间等级" prop="roomGrade">
          <el-input v-model="form.roomGrade" placeholder="请输入房间等级" />
        </el-form-item>
        <el-form-item label="数量" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="剩余数量" prop="remainingQuantity">
          <el-input v-model="form.remainingQuantity" placeholder="请输入剩余数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改房间类型配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="床位数" prop="bedQuantity">
          <el-input v-model="form.bedQuantity" placeholder="请输入床位数" />
        </el-form-item>
        <el-form-item label="数量" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="剩余数量" prop="remainingQuantity">
          <el-input v-model="form.remainingQuantity" placeholder="请输入剩余数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listRoomByPage, listType, getType, addType, updateType, delType } from '@/api/manage/roomtype'
export default {
  components: {},
  props: [],
  data() {
    return {
      // 房间等级
      typeOptions: undefined,
      // 遮罩层
      loading: true,
      // 字典表格数据
      typeList: [],
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      add: '',
      // 是否显示弹出层
      open: false,
      openadd: false,
      // 表单参数
      form: {},
      // 当前页
      pageNum: 0,
      // 每页数据量
      pageSize: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomTypeId: undefined,
        price: undefined,
        bedQuantity: undefined,
        roomGrade: undefined,
        amount: undefined,
        remainingQuantity: undefined
      },
      rules: {
        roomTypeId: [{
          required: true,
          message: '房间类型id不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.fetchData()
    listType().then((response) => {
      this.typeOptions = response.data
    })
  },
  mounted() {},
  methods: {
    /** 查询房间类型列表 */
    fetchData() {
      this.loading = true
      listRoomByPage(this.queryParams).then((response) => {
        this.typeList = response.data.list
        this.total = response.data.total
        this.pageNum = response.data.pageNum
        this.pageSize = response.data.pageSize
        this.loading = false
      })
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.fetchData()
    },
    // 表单重置
    resetForm() {
      this.form = {
        roomTypeId: undefined,
        price: undefined,
        bedQuantity: undefined,
        roomGrade: undefined,
        amount: undefined,
        remainingQuantity: undefined
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        roomTypeId: undefined,
        price: undefined,
        bedQuantity: undefined,
        roomGrade: undefined,
        amount: undefined,
        remainingQuantity: undefined
      }
      this.fetchData()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.fetchData()
      this.$message.success('查询成功')
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.openadd = false
      this.resetForm()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm()
      this.openadd = true
      this.add = '新增房间类型'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm()
      getType(row.roomTypeId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改房间类型'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '修改房间类型') {
            updateType(this.form).then((response) => {
              this.open = false
              this.title = ''
              this.fetchData()
              this.$message.success('修改成功') // 后端返回消息字符串
            })
          } else {
            addType(this.form).then((response) => {
              const flag = response.data
              if (flag) {
                this.$message.success('添加成功')
                this.openadd = false
                this.add = ''
                this.fetchData()
              } else {
                this.$message.error('操作失败,无法添加相同的房间类型')
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除名称为"' + row.roomGrade + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delType(row.roomTypeId)
        })
        .then(() => {
          this.fetchData()
          this.$message.success('删除成功')
        })
        .catch(() => {})
    }
  }
}

</script>
<style>
</style>
